package com.cecsmsserve.controller;

import com.cecsmsserve.entity.News;
import com.cecsmsserve.entity.NewsComment;
import com.cecsmsserve.service.NewsCategoryService;
import com.cecsmsserve.service.NewsCommentService;
import com.cecsmsserve.service.NewsService;
import com.cecsmsserve.util.result.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    private static final Logger log = LoggerFactory.getLogger(NewsController.class);
    
    @Autowired
    private NewsService newsService;
    
    @Autowired
    private NewsCommentService commentService;
    
    @Autowired
    private NewsCategoryService categoryService;
    
    /**
     * 添加新闻
     */
    @PostMapping
    public CommonResult addNews(@RequestBody News news, @RequestParam List<Integer> categoryIds) {
        try {
            return newsService.addNews(news, categoryIds);
        } catch (Exception e) {
            log.error("添加新闻失败：", e);
            return CommonResult.failed("添加新闻失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新新闻
     */
    @PutMapping
    public CommonResult updateNews(@RequestBody News news, @RequestParam List<Integer> categoryIds) {
        try {
            return newsService.updateNews(news, categoryIds);
        } catch (Exception e) {
            log.error("更新新闻失败：", e);
            return CommonResult.failed("更新新闻失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除新闻
     */
    @DeleteMapping("/{id}")
    public CommonResult deleteNews(@PathVariable Integer id) {
        try {
            return newsService.deleteNews(id);
        } catch (Exception e) {
            log.error("删除新闻失败：", e);
            return CommonResult.failed("删除新闻失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取新闻详情
     */
    @GetMapping("/{id}")
    public CommonResult getNewsDetail(@PathVariable Integer id) {
        try {
            // 增加浏览量
            newsService.increaseViewCount(id);
            return newsService.getNewsDetail(id);
        } catch (Exception e) {
            log.error("获取新闻详情失败：", e);
            return CommonResult.failed("获取新闻详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取新闻列表(后台管理)
     */
    @GetMapping("/admin/list")
    public CommonResult getNewsList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer status) {
        try {
            return newsService.getNewsList(pageNum, pageSize, keyword, categoryId, status);
        } catch (Exception e) {
            log.error("获取新闻列表失败：", e);
            return CommonResult.failed("获取新闻列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取前台新闻列表
     */
    @GetMapping("/list")
    public CommonResult getFrontNewsList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer categoryId) {
        try {
            return newsService.getFrontNewsList(pageNum, pageSize, categoryId);
        } catch (Exception e) {
            log.error("获取新闻列表失败：", e);
            return CommonResult.failed("获取新闻列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取置顶新闻
     */
    @GetMapping("/top")
    public CommonResult getTopNews() {
        try {
            return newsService.getTopNews();
        } catch (Exception e) {
            log.error("获取置顶新闻失败：", e);
            return CommonResult.failed("获取置顶新闻失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新新闻状态
     */
    @PutMapping("/status")
    public CommonResult updateNewsStatus(
            @RequestParam Integer id,
            @RequestParam Integer status) {
        try {
            return newsService.updateNewsStatus(id, status);
        } catch (Exception e) {
            log.error("更新新闻状态失败：", e);
            return CommonResult.failed("更新新闻状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新新闻置顶状态
     */
    @PutMapping("/top")
    public CommonResult updateNewsTopStatus(
            @RequestParam Integer id,
            @RequestParam Integer isTop) {
        try {
            return newsService.updateNewsTopStatus(id, isTop);
        } catch (Exception e) {
            log.error("更新新闻置顶状态失败：", e);
            return CommonResult.failed("更新新闻置顶状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 添加评论
     */
    @PostMapping("/comment")
    public CommonResult addComment(@RequestBody NewsComment comment) {
        try {
            return commentService.addComment(comment);
        } catch (Exception e) {
            log.error("添加评论失败：", e);
            return CommonResult.failed("添加评论失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除评论
     */
    @DeleteMapping("/comment/{id}")
    public CommonResult deleteComment(
            @PathVariable Integer id,
            @RequestParam Integer userId) {
        try {
            return commentService.deleteComment(id, userId);
        } catch (Exception e) {
            log.error("删除评论失败：", e);
            return CommonResult.failed("删除评论失败：" + e.getMessage());
        }
    }
    
    /**
     * 管理员删除评论
     */
    @DeleteMapping("/admin/comment/{id}")
    public CommonResult adminDeleteComment(@PathVariable Integer id) {
        try {
            return commentService.adminDeleteComment(id);
        } catch (Exception e) {
            log.error("删除评论失败：", e);
            return CommonResult.failed("删除评论失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取新闻评论列表
     */
    @GetMapping("/comment/list/{newsId}")
    public CommonResult getNewsComments(
            @PathVariable Integer newsId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            return commentService.getNewsComments(newsId, pageNum, pageSize);
        } catch (Exception e) {
            log.error("获取评论列表失败：", e);
            return CommonResult.failed("获取评论列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取分类列表
     */
    @GetMapping("/category/list")
    public CommonResult getCategoryList() {
        try {
            return categoryService.getCategoryList();
        } catch (Exception e) {
            log.error("获取分类列表失败：", e);
            return CommonResult.failed("获取分类列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 添加分类
     */
    @PostMapping("/category")
    public CommonResult addCategory(@RequestBody com.cecsmsserve.entity.NewsCategory category) {
        try {
            return categoryService.addCategory(category);
        } catch (Exception e) {
            log.error("添加分类失败：", e);
            return CommonResult.failed("添加分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新分类
     */
    @PutMapping("/category")
    public CommonResult updateCategory(@RequestBody com.cecsmsserve.entity.NewsCategory category) {
        try {
            return categoryService.updateCategory(category);
        } catch (Exception e) {
            log.error("更新分类失败：", e);
            return CommonResult.failed("更新分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除分类
     */
    @DeleteMapping("/category/{id}")
    public CommonResult deleteCategory(@PathVariable Integer id) {
        try {
            return categoryService.deleteCategory(id);
        } catch (Exception e) {
            log.error("删除分类失败：", e);
            return CommonResult.failed("删除分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新分类状态
     */
    @PutMapping("/category/status")
    public CommonResult updateCategoryStatus(
            @RequestParam Integer id,
            @RequestParam Integer status) {
        try {
            return categoryService.updateCategoryStatus(id, status);
        } catch (Exception e) {
            log.error("更新分类状态失败：", e);
            return CommonResult.failed("更新分类状态失败：" + e.getMessage());
        }
    }
} 